using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ForumClient;

namespace ForumExplorer
{
    public partial class ForumExplorer : Form
    {
        public ForumExplorer()
        {
            InitializeComponent();
        }

        private void ForumExplorer_Load(object sender, EventArgs e)
        {
            foreach (DataRow dr in metadata.Sites.Rows)
            {
                TreeNode node = treeViewForums.Nodes.Add(dr["SiteName"] as string);
                node.Tag = dr;
                AddExpandNode(node);
            }
        }

        private void AddExpandNode(TreeNode node)
        {
            TreeNode expandnode = node.Nodes.Add("Expand");
        }

        private ForumMetaData metadata = new ForumMetaData();

        private void treeViewForums_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (true == webBrowserForum.IsBusy)
            {
                e.Cancel = true;
                return;
            }

            TreeView tv = sender as TreeView;
            TreeNode node = e.Node;
            if (1 == node.Nodes.Count && 0 == String.Compare(node.Nodes[0].Text, "Expand", true))
            {
                DataRow row = node.Tag as DataRow;
                if (null != row)
                {
                    if (0 == String.Compare(row.Table.TableName, "Sites", false))
                    {
                        node.Nodes.Clear();
                        string url = String.Format("http://forums.microsoft.com/{0}/default.aspx?SiteID={1}", node.Text, node.Tag.ToString());
                        node.Text += " (expanding...)";
                        webBrowserForum.Tag = node;
                        webBrowserForum.Navigate(url);
                    }
                    else if (0 == String.Compare(row.Table.TableName, "Groups", false))
                    {
                        node.Nodes.Clear();
                        string filter = String.Format("GroupID={0}", row["GroupID"].ToString());
                        DataRow[] forums = metadata.Forums.Select(filter);
                        foreach (DataRow forumrow in forums)
                        {
                            TreeNode child = node.Nodes.Add(forumrow["ForumName"].ToString());
                            child.Tag = forumrow;
                        }
                        node.Expand();
                    }
                }
            }
        }

        private void webBrowserForum_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            TreeNode node = (TreeNode)webBrowserForum.Tag;
            DataRow siterow = node.Tag as DataRow;

            if (0 == String.Compare(siterow.Table.TableName, "Sites", true) &&
                false == (bool)siterow["SiteIsLoaded"])
            {
                node.Nodes.Clear();
                TreeNode child;

                Parser p = new Parser();
                p.ParseForumList(webBrowserForum.Document, metadata.Groups, metadata.Forums, siterow["SiteName"].ToString());

                String filter = String.Format("SiteID={0}", siterow["SiteID"]);

                foreach (DataRow row in metadata.Groups.Select(filter))
                {
                    child = node.Nodes.Add(row["GroupName"].ToString());
                    child.Tag = row;
                    AddExpandNode(child);
                }
                node.Expand();
                node.Text = siterow["SiteName"].ToString();
                siterow["SiteIsLoaded"] = true;
            }
        }

        private void treeViewForums_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataRow row = e.Node.Tag as DataRow;

            string url = null;
            if (0 == String.Compare(row.Table.TableName, "Sites", false))
            {
                url = String.Format("http://forums.microsoft.com/{0}/default.aspx?SiteID={1}", row["SiteName"], row["SiteID"].ToString());
            }
            else if (0 == String.Compare(row.Table.TableName, "Groups", false))
            {
                DataRow[] siterows = metadata.Sites.Select(String.Format("SiteID={0}", row["SiteID"].ToString()));
                url = String.Format("http://forums.microsoft.com/{0}/default.aspx?ForumGroupID={1}&SiteID={2}", siterows[0]["SiteName"], row["GroupID"], siterows[0]["SiteID"].ToString());
            }
            else if (0 == String.Compare(row.Table.TableName, "Forums", false))
            {
                DataRow[] grouprows = metadata.Groups.Select(String.Format("GroupID={0}", row["GroupID"].ToString()));
                DataRow[] siterows = metadata.Sites.Select(String.Format("SiteID={0}", grouprows[0]["SiteID"].ToString()));
                url = String.Format("http://forums.microsoft.com/{0}/ShowForum.aspx?ForumID={1}&SiteID={2}", siterows[0]["SiteName"], row["ForumID"], siterows[0]["SiteID"].ToString());
            }

            if (null != url)
            {
                webBrowserForum.Tag = e.Node;
                webBrowserForum.Navigate(url);
            }
        }

        private void treeViewForums_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            if (true == webBrowserForum.IsBusy)
            {
                e.Cancel = true;
                return;
            }
        }
    }
}